package com.example;

import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;


public class Main {
    @Test
    public void testReadXml() throws ParserConfigurationException, IOException, SAXException {
        File file = new File("src/main/resources/IPConfig.xml");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        Document parse = documentBuilder.parse(file);
        parse.getDocumentElement().normalize();

        String nodeName = parse.getDocumentElement().getNodeName();
        String nodeValue = parse.getDocumentElement().getNodeValue();
        System.out.println("Root element: " + parse.getDocumentElement().getNodeName());
        System.out.println("Root element value: " + parse.getDocumentElement().getNodeValue());

        NodeList server = parse.getElementsByTagName("server");

        System.out.println("Server: " + server.item(0).getTextContent());

        Node item = server.item(0);
        String nodeName1 = item.getNodeName();
        System.out.println("Node name: " + nodeName1);
        Node firstChild = item.getFirstChild();
        System.out.println("First child: " + firstChild.getNodeName());

    }
}